//
//  WLLEmoticonPageView.swift
//  WLLWeibo
//
//  Created by 王乐乐 on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
//cell 重用 ID
let WLLEmoticonPageViewCellId = "WLLEmoticonPageViewCellId"


class WLLEmoticonPageView: UICollectionView {

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
     //MARK: - 设置视图
    func setupUI() {
//        backgroundColor = RandomColor()
        //注册
        registerClass(WLLEmoticonPageViewCell.self , forCellWithReuseIdentifier: WLLEmoticonPageViewCellId)
        
        dataSource = self
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //设置 layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        //设置 itemsize
        layout.itemSize = self.frame.size
        
        //设置行间距
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        //滚动方向
        
        layout.scrollDirection = .Horizontal
        
    }
    
}

extension WLLEmoticonPageView : UICollectionViewDataSource{
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return WLLEmoticonTools.sharedTools.allEmoticons.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return WLLEmoticonTools.sharedTools.allEmoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WLLEmoticonPageViewCellId, forIndexPath: indexPath) as! WLLEmoticonPageViewCell
        //赋值
        cell.indexPath = indexPath
        cell.backgroundColor = self.backgroundColor
        cell.emoticons = WLLEmoticonTools.sharedTools.allEmoticons[indexPath.section][indexPath.item]
        return cell
        
    }
    
    
    
}